package cc.mrbird.febs.common.formatter;

import cc.mrbird.febs.common.annotation.Desensitized;
import cc.mrbird.febs.common.enums.SensitiveType;
import org.springframework.format.AnnotationFormatterFactory;
import org.springframework.format.Parser;
import org.springframework.format.Printer;

import org.springframework.format.Formatter;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

/**
 *
 *
 * @author dtwei
 */
public class DesensitizedAnnotationFormatterFactory implements AnnotationFormatterFactory<Desensitized> {
    /**
     * 返回为处理的变量的类型
     *
     * @return
     */
    @Override
    public Set<Class<?>> getFieldTypes() {
        Set<Class<?>> hashSet = new HashSet<>();
        hashSet.add(String.class);
        hashSet.add(BigDecimal.class);
        return hashSet;
    }

    @Override
    public Printer<?> getPrinter(Desensitized desensitized, Class<?> aClass) {
        if(aClass.equals(BigDecimal.class)) {
            return getNumFormatter(desensitized);
        } else {
            return getStringFormatter(desensitized);
        }
    }

    @Override
    public Parser<?> getParser(Desensitized desensitized, Class<?> aClass) {
        if(aClass.equals(BigDecimal.class)) {
            return getNumFormatter(desensitized);
        } else {
            return getStringFormatter(desensitized);
        }
    }

    private DesensitizedFormatterOfString getStringFormatter(Desensitized desensitized) {
        DesensitizedFormatterOfString formatter = new DesensitizedFormatterOfString();
        formatter.setTypeEnum(desensitized.type());
        return formatter;
    }

    private DesensitizedFormatterOfNum getNumFormatter(Desensitized desensitized) {
        DesensitizedFormatterOfNum formatter = new DesensitizedFormatterOfNum();
        formatter.setTypeEnum(desensitized.type());
        return formatter;
    }
}